<?php
namespace app\admin\controller;
use think\Db;
use think\facade\Request;
use app\admin\controller\Section;
class Meeting extends Common
{
    /**会议列表
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function lists(){
        if (request()->isAjax()) {

            $keyword = input('post.keyword');
            $page =input('page',1);
            $pageSize =input('limit',config('pageSize'));

            $list = Db::name('meeting')
                ->where('name', 'like', "%" . $keyword . "%")
                ->order('create_time desc')
                ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                ->toArray();

            if (! empty($list)) {

                foreach ($list['data'] as $k => $v) {

                    if($v['person']=="0"){
                        $list['data'][$k]['person'] = '全部';
                    }else{
                        $arr = json_decode($v['person'],true);
                        $str = '';
                        foreach ($arr as $val){

                            $str .= Db::name('teacher')->where('id',$val)->value('true_name');
                            $str .= ",";
                        }
                        $list['data'][$k]['person'] = $str;
                    }


                    if((time()>strtotime($v['meeting_start_time'])) && (time()<strtotime($v['meeting_end_time'])) && ($v['status']!=-1) ){
                        $list['data'][$k]['status'] = 1;
                        Db::name("meeting")->where('id',$v['id'])->setField('status',1);
                    }elseif ((time()>strtotime($v['meeting_end_time'])) && ($v['status']!=-1)){
                        $list['data'][$k]['status'] = 2;
                        Db::name("meeting")->where('id',$v['id'])->setField('status',2);
                    }
                    $list['data'][$k]['create_time'] = toDate($v['create_time']);

                }

            }

            return $result = ['code'=>0,'msg'=>'成功!','data'=>$list['data'],'count'=>$list['total'],'rel'=>1];
        }


        return $this->fetch('meeting_list');
    }

    /**添加会议
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function meetingAdd(){

        if(Request::isAjax()){

            $data = input('post.');

            $data['creater']     = session('username');
            $data['create_time'] = time();

            Db::name('meeting')->insert($data);
            $meetingname =  Db::name('meeting')->where('id', $data['id'])->value('name');

            $log = [
                'content'=>'添加会议'.$meetingname
            ];
            $wed = new Weblog();
            $wed->insert($log);
            $this->success('成功',url('lists'));

        }else{
            $deviceGroup = Db::name('device')->where('is_del',0)->select();
            $this->assign('deviceGroup',$deviceGroup);
            $this->assign('title',lang('add').'会议');
            $this->assign('info','null');
            return view('meetingAdd');
        }
    }

    /**编辑规则
     * @return \think\response\Json|\think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function meetingEdit(){
        if(Request::isAjax()){

            $info = Db::name('meeting')->where('id',input('id'))->find();


            if($info['person']=="0"){

            }else{

                $arr = json_decode($info['person'],true);

                foreach ($arr as $k=>$val){
                    $true_name = Db::name('teacher')->where('id',$val)->value('true_name');
                    $d[$k]['true_name']= $true_name;
                    $d[$k]['id']= $val;
                }

                $info['person'] = $d;


            }

            return $info['person'];

        }else{

            $info = Db::name('meeting')->where('id',input('id'))->find();

            $info['meeting_time'] = substr($info['sign_start_time'],0,strpos($info['sign_start_time'], ' '));
            $info['sign_start_time'] = substr($info['sign_start_time'],strpos($info['sign_start_time'],' ')+1);
            $info['sign_end_time'] = substr($info['sign_end_time'],strpos($info['sign_end_time'],' ')+1);
            $info['meeting_start_time'] = substr($info['meeting_start_time'],strpos($info['meeting_start_time'],' ')+1);
            $info['meeting_end_time'] = substr($info['meeting_end_time'],strpos($info['meeting_end_time'],' ')+1);

            $deviceGroup = Db::name('device')->where('is_del',0)->select();
            $this->assign('deviceGroup',$deviceGroup);
            $this->assign('info', $info);
            $this->assign('title',lang('edit').'会议');
            $this->assign('id',input('id'));
            return view('meetingEdit');
        }
    }

    /**保存编辑
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function meetingSave(){

        if(Request::isAjax()){
            $data = input('post.');

            Db::name('meeting')->update($data);

            $log = [
                'content'=>'编辑会议'.$data['name']
            ];
            $wed = new Weblog();
            $wed->insert($log);
            $this->success('成功',url('lists'));

        }
    }

    /**取消会议
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function cancel(){

        $meeting_id=input('post.meeting_id');
        if (session('aid')==1){

            Db::name('meeting')->where('id', $meeting_id)->update(['status' => -1]);
            $meetingname =  Db::name('meeting')->where('id', $meeting_id)->value('name');

            $log = [
                'content'=>'取消会议'.$meetingname
            ];
            $wed = new Weblog();
            $wed->insert($log);
            $this->success('取消成功!');
        }else{
            $this->error('您没有取消会议的权限!');
        }
    }


    /**教师列表
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function teacher(){

        if (request()->isAjax()) {

            input('post.id')?$section_id = input('id'):$section_id="";
            if($section_id!=''){

                $section = new Section();
                $str = $section_id;

                foreach ($section->showCate($section_id) as $k=>$v){
                    $str .= ",".$v['section_id'];
                }

                $list = Db::name('teacher t')
                    ->join('section s','t.section_id=s.section_id','left')
                    ->field('t.teacher_no,t.true_name,t.sex,t.id,s.title')
                    ->where('t.is_del',0)
                    ->where('s.is_del',0)
                    ->where('t.section_id','in',$str)
                    ->order('t.section_id desc')
                    ->select();

            }else{

                $list = Db::name('teacher t')
                    ->join('section s','t.section_id=s.section_id','left')
                    ->field('t.teacher_no,t.true_name,t.sex,t.id,s.title')
                    ->where('t.is_del',0)
                    ->where('s.is_del',0)
                    ->order('t.section_id desc')
                    ->select();

            }

            return $result = ['code'=>0,'msg'=>'成功!','data'=>$list];
        }

        $section = new Section();
        $this->assign('section',$section->showCate());
        return $this->fetch('teacher');
    }

}